package cn.enine.tbcharge.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cn.enine.tbcharge.R;
import cn.enine.tbcharge.adapter.CommonAdapter;
import cn.enine.tbcharge.adapter.ViewHolder;
import cn.enine.tbcharge.base.MyBaseActivity;
import cn.enine.tbcharge.pojo.AppSfMain;
import cn.enine.tbcharge.pojo.AppSfSub;
import cn.enine.tbcharge.pojo.AppXmpy;
import cn.enine.tbcharge.pojo.RetMsg;
import cn.enine.tbcharge.util.HttpUrl;
import cn.enine.tbcharge.util.LvHeightUtil;
import cn.enine.tbcharge.util.MyToastUtil;
import cn.enine.tbcharge.util.NetWorkUtils;
import cn.enine.tbcharge.util.RegexUtils;
import cn.enine.tbcharge.util.SharedPreferencesUtils;
import cn.enine.tbcharge.util.StringUtils;
import cn.enine.tbcharge.util.UserClient;

public class XmaddActivity extends MyBaseActivity {
    Context con = XmaddActivity.this;
    AppXmpy obj;
    private TextView  xmmc, xmbh,title, saveBtn;
    private EditText pym;
    LinearLayout back;
    String url = "";
    ListView listView;
    ZLoadingDialog dialog;//加载动画

    List<AppSfSub> list;
    private int pageNum1 = 1;// 无关键字页数
    private EditText key;

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_xmadd);
        if (NetWorkUtils.isNetworkConnected(con)) {
            obj = (AppXmpy) getIntent().getExtras().getSerializable("b");
            init(obj);
        }
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
    }

    private void init(final AppXmpy obj) {
        xmmc = (TextView) findViewById(R.id.xmmc);
        if (obj != null && !StringUtils.isEmpty(obj.getYyxmmc())) {
            xmmc.setText(obj.getYyxmmc());
        }
        xmbh = (TextView) findViewById(R.id.xmbh);
        if (obj != null && !StringUtils.isEmpty(obj.getYyxmbm())) {
            xmbh.setText(obj.getYyxmbm());
        }
        pym = (EditText) findViewById(R.id.pym);
        if (obj != null && !StringUtils.isEmpty(obj.getSzm())) {
            pym.setText(obj.getSzm());
        }
        pym.requestFocus();
        pym.setSelection(pym.getText().length());
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity(new Intent(con, XmlistActivity.class));
            }
        });
        title = (TextView) findViewById(R.id.title);
        saveBtn = (TextView) findViewById(R.id.uname);
        title.setText("收费新增");
        saveBtn.setText("保存");
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //boolean validate = BinderUtil.copyDataToView(grjbxx_fh.this, obj);
                String xmmcText = xmmc.getText().toString();
                String xmbhText = xmbh.getText().toString();
                String pymText = pym.getText().toString();

                if (null == pymText || "".equals(pymText)) {
                    MyToastUtil.ShowToast(con, "请输入正确拼音码");
                }else {//验证通过
                    obj.setYyxmmc(xmmcText);
                    obj.setYyxmbm(xmbhText);
                    obj.setSzm(pymText);

                    if (NetWorkUtils.isNetworkConnected(con)) {
                        //网络连接时
                        RequestParams ps = new RequestParams();
                        ps.add("entity.NID", obj.getNid());
                        ps.add("entity.SZM", pymText);
                        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                        ps.add("type", "2");
                        url = HttpUrl.EditXm;
                        UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(String content) {
                                super.onSuccess(content);
                                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                if (ret.getCode().equals("200")) {
                                    MyToastUtil.ShowToast(con, "成功");
                                    Bundle b = new Bundle();
                                    obj.setNid(ret.getRd());
                                    b.putSerializable("b", obj);
                                    finish();
                                }
                            }

                            @Override
                            public void onFailure(Throwable error) {
                                super.onFailure(error);
                                MyToastUtil.ShowToast(con, "失败");
                            }
                        });
                    }else{
                        //网络不通,保存本地
//                        obj.save();
                    }
                }
            }
        });
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

}
